package com.eternal.base.database.dao;

import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import com.eternal.base.database.entity.Log;

import java.util.List;

/**
 * Created by Administrator
 * On 2020/5/13
 * Description:
 *
 * @author Administrator
 */
@Dao
public interface LogDao {
    /**
     * 加载指定时间内的日志
     *
     * @param start 起始
     * @param end   结束
     * @return 日志
     */
    @Query(value = "select * from log where mac=:mac and time>=:start and time<=:end order by time desc")
    List<Log> loadLogByTime(String mac, long start, long end);

    /**
     * 添加一条日志
     *
     * @param log 日志
     */
    @Insert()
    void insertLog(Log log);

    /**
     * 加载指定设备的
     *
     * @param mac    设备mac
     * @param
     * @return 日志
     */
    @Query("select * from log where mac=:mac order by time desc")
    DataSource.Factory<Integer, Log> loadAllMacByModel(String mac);

    /**
     * 获取最后添加日志
     *
     * @param mac 设备mac
     * @return 日志
     */
    @Query("select * from log where mac=:mac order by time desc")
    Log loadFirstLog(String mac);

    @Query("select * from log where mac=:mac and port=:port order by time desc")
    Log loadFirstELog(String mac, byte port);

    @Query("select * from log where mac=:mac order by time desc")
    Log loadFirstCLog(String mac);

    /**
     * 根据类型和模式获取日志
     *
     * @param mac    设备mac
     * @param types  日志类型
     * @param models 模式
     * @return 日志
     */
    @Query("select * from log where mac=:mac and port=:port and (logType in (:types) or (logType=0 and model in (:models))) order by time desc")
    DataSource.Factory<Integer, Log> loadByTypeAndModel(String mac, byte port, List<Byte> types, List<Byte> models);

    @Query("select * from log where mac=:mac and model in (:types) order by time desc")
    DataSource.Factory<Integer, Log> loadCByType(String mac, List<Byte> types);

    /**
     * 根据类型获取日志
     *
     * @param mac   设备mac
     * @param types 日志类型
     * @return 日志
     */
    @Query("select * from log where mac=:mac and port=:port and logType in (:types) order by time desc")
    DataSource.Factory<Integer, Log> loadByType(String mac, byte port, List<Byte> types);

    @Query("select count(1) from log where mac=:mac and port=:port and time>:time")
    int numByMacAndTime(String mac, byte port, long time);

    @Query("delete from log where mac=:mac")
    void clear(String mac);
}
